package cn.itsource.controller;

import cn.itsource.domain.Teacher;
import cn.itsource.service.ITeacherService;
import cn.itsource.util.ExcelUtil;
import cn.itsource.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

//@Slf4j
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    private static final Logger log = LoggerFactory.getLogger(TeacherController.class);
    @Autowired
    private ITeacherService teacherService;

    /**
     * 保存教师信息
     * @param teacher 教师对象
     * @return JSONResult<Teacher> 返回保存结果
     */
    @PutMapping("/save")
    public JSONResult<Teacher> save(@RequestBody Teacher teacher) {
        System.out.println(teacher);
        try {
            teacherService.save(teacher);
            return JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    /**
     * 更新教师信息
     * @param teacher 教师对象
     * @return JSONResult<Teacher> 返回更新结果
     */
    @PostMapping("/update")
    public JSONResult<Teacher> update(@RequestBody Teacher teacher) {
        try {
            teacherService.update(teacher);
            return JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    /**
     * 删除教师信息
     * @param id 教师ID
     * @return JSONResult<Teacher> 返回删除结果
     */
    @DeleteMapping("/dlt/{id}")
    public JSONResult<Teacher> dlt(@PathVariable("id") Long id) {
        try {
            teacherService.dlt(id);
            return JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    /**
     * 获取教师信息
     * @param id 教师ID
     * @return JSONResult<Teacher> 返回教师信息
     */
    @GetMapping("/get/{id}")
    public JSONResult<Teacher> get(@PathVariable("id") Long id) {
        log.info("{} get 方法被调用了，参数为：{}", new Date(), id);
        try {
            teacherService.get(id);
            return JSONResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    /**
     * 获取所有教师信息
     * @return JSONResult<Object> 返回所有教师信息
     */
    @GetMapping("/getAll")
    public JSONResult<Object> getAll() {

        log.info("{} getAll 方法被调用了：", new Date());

        try {
            List<Teacher> all = teacherService.getAll();
            return JSONResult.ok().setData(all);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.fail("输入信息有误"+e.getMessage());
        }
    }

    /**
     * 批量导入教师信息
     * @param file 导入的Excel文件
     * @return JSONResult<Teacher> 返回导入结果
     */
    @PostMapping("/importExcel")
    public JSONResult<Teacher> importExcel(MultipartFile file) throws IOException {
        try {
            List<Teacher> teachers = ExcelUtil.importExcel(file, 1, 1, Teacher.class);
            teacherService.batchInsert(teachers);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail("导入失败，输入信息有误"+e.getMessage());
        }
    }

    /**
     * 导出教师信息
     * @param response HttpServletResponse对象
     * @throws IOException 抛出IO异常
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelUtil.exportExcel(teacherService.getAll(),"老师列表","老师",Teacher.class,"老师列表",response);
    }
}
